package com.stlm2.dems.service;

import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.PriceRecord;
import com.stlm2.dems.dao.PriceRecordDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("priceRecordService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class PriceRecordService extends BaseServiceImpl<PriceRecord> {

	@Resource
	private PriceRecordDao priceRecordDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;

	@Override
	protected BaseDao <PriceRecord> getDao() {
		return priceRecordDao;
	}

	/*
	*zlq
	* 获取价格走势
	* goodsId:商品ID
	* specificationId：规格ID
	*
	 */
	public String getPriceRecord(ModelMap model,String goodsId,String specificationId){
		if (!NumberUtils.isDigits(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		PriceRecord priceRecord=new PriceRecord();
		if(ddmGoods!=null){
			priceRecord.setGoodsId(ddmGoods.getId());
			if (NumberUtils.isDigits(specificationId)) {
				priceRecord.setSpeciflcationId(Integer.parseInt(specificationId));
			}
			List <PriceRecord> priceRecordList=priceRecordDao.queryPageView(-1,-1,priceRecord).getRecords();
			priceRecord=priceRecordDao.getRecentlyOne(priceRecord);
			if(priceRecord==null){
				return 	CodeContent.getModelByCode(0,"价格走势异常", model);
			}
			return 	CodeContent.getModelByCode(1,"success", modelList(priceRecordList,priceRecord));

		}
		return 	CodeContent.getModelByCode(1,"success", model);
	}

	public ModelMap modelList(List<PriceRecord> priceRecordList,PriceRecord priceRecord) {
		ModelMap datemodel = new ModelMap();
		List<String> dateList=new ArrayList<String>();
		List<String> priceList=new ArrayList<String>();
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
		Date date=new Date();
		Calendar cal = Calendar.getInstance();
			for(int i=7;i>1;i--){
				String priceStr=priceRecord.getPrice().toString();
				cal.setTime(date);
				cal.set(Calendar.DATE, cal.get(cal.DATE) - i);
				for (PriceRecord priceRecord1:priceRecordList){
					String recordDate=formatter.format(priceRecord1.getUpdateTime());
					if(recordDate.equals(formatter.format(cal.getTime()))){
						priceStr=priceRecord1.getPrice().toString();
					}
				}
				dateList.add(formatter.format(cal.getTime()));
				priceList.add(priceStr);
			}

		datemodel.put("date",dateList);
		datemodel.put("price",priceList);
		return datemodel;
	}


}